package de.tabuma.formcreator.javascript.logik;

import de.tabuma.formcreator.entity.*;

import java.util.logging.*;
import java.util.*;

/**
  * @author $Author: $
  * @version $Revision: $
  * @since $Date: $
 */
public class EntityFacade {
    private static final Logger log = Logger.getLogger(EntityFacade.class.getName());

    public static String getAllObject(FormStructur formStructur) {
        String str = "";
        str += "\n";
        str += "<!--Start object discription for "+formStructur.getName()+"-->\n";

        str += createEntity(formStructur);
        for(int i=0;i<formStructur.getFormStructur().size();i++) {
            FormStructur fs = formStructur.getFormStructur().get(i);
            str += getGetter(fs);
            if(!fs.isSimpleType()) {
                str += addList(fs);
                str += removeElementInList(fs);
                str += insertElementIntoList(fs);
            }
            else {
                str += getSetter(fs);
            }
        }
        str += getFromObjectToXmlFunction(formStructur);
        str += getFromXmlToObjectFunction(formStructur);

        str += "\tvar "+JSLib.getObjectName(formStructur)+" = new "+JSLib.getObjectContructurName(formStructur)+"();\n";
        str += "\tvar "+JSLib.getObjectCounter(formStructur)+" = -1;\n";

        str += "<!--End object discription for "+formStructur.getName()+"-->\n";
        str += "\n";
        return str;
    }

    public static String createEntity(FormStructur fs) {
        String str = "\t"+JSLib.getObjectContructurName(fs)+" = function() {\n";
        for(int i=0;i<fs.getFormStructur().size();i++) {
            FormStructur fs1 = fs.getFormStructur().get(i);
            if(fs1.isSimpleType()) {
                str += "\t\tthis."+JSLib.getObjectField(fs1)+"=\"\"; //"+fs1.getName()+"\n";
            }
            else {
                str += "\t\tthis."+JSLib.getObjectField(fs1)+"=new Array(); //"+fs1.getName()+"\n";
            }
        }
        str += "\t}\n";
        return str;
    }
    
    public static String getGetter(FormStructur fs) {
        String str = "\t"+JSLib.getObjectContructurName(fs.getHeadStructur())+".prototype."+JSLib.getObjectGetterName(fs)+" = function() {\n";
        str += "\t\treturn this."+JSLib.getObjectField(fs)+";\n";
        str += "\t}\n";
        return str;
    }

    public static String getSetter(FormStructur fs) {
        String str = "\t"+JSLib.getObjectContructurName(fs.getHeadStructur())+".prototype."+JSLib.getObjectSetterName(fs)+" = function(y) {\n";
        str += "\t\tthis."+JSLib.getObjectField(fs)+"=y;\n";
        str += "\t}\n";
        return str;
    }

    public static String addList(FormStructur fs) {
        String str = "\t"+JSLib.getObjectContructurName(fs.getHeadStructur())+".prototype."+JSLib.getObjectAddList(fs)+" = function(y) {\n";
        str += "\t\tthis."+JSLib.getObjectField(fs)+".push(y);\n";
        str += "\t}\n";
        return str;
    }

    public static String removeElementInList(FormStructur fs) {
        String str = "\t"+JSLib.getObjectContructurName(fs.getHeadStructur())+".prototype."+JSLib.getObjectRemoveList(fs)+" = function(y) {\n";
        str += "\t\t"+JSLib.getRemoveElementFromArrayList()+"(y);\n";
        str += "\t}\n";
        return str;
    }

    public static String insertElementIntoList(FormStructur fs) {
        String str = "\t"+JSLib.getObjectContructurName(fs.getHeadStructur())+".prototype."+JSLib.getObjectInsertElementIntoList(fs)+" = function(object, y) {\n";
        str += "\t\tthis."+JSLib.getObjectField(fs)+"[y] = object;\n";
        str += "\t}\n";
        return str;
    }

    public static String removeArray() {
        String str = "";
        str += "\tfunction "+JSLib.getRemoveElementFromArrayList()+"(array,removeId) {\n";
        str += "\t\tarray1 = new Array[array.length-1];\n";
        str += "\t\tj=0;\n";
        str += "\t\tfor(i=0;i<array1.length;i++) {\n";
        str += "\t\t\tif(i==removeId) {\n";
        str += "\t\t\t\tj=1;\n";
        str += "\t\t\t}\n";
        str += "\t\t\tarray1[i]=array[i+j];\n";
        str += "\t\t}\n";
        str += "\t\treturn array1;\n";
        str += "\t}\n";
        return str;
    }

    public static String getFromObjectToXmlFunction(FormStructur fs) {
        String str = "";
        str += "\t"+JSLib.getObjectContructurName(fs)+".prototype."+JSLib.getStructurAsXML()+" = function() {\n";
        str += "\t\txml = '';\n";
        str += "\t\t"+JSLib.getLoggingFunction()+"('start "+JSLib.getObjectContructurName(fs)+" "+JSLib.getStructurAsXML()+"');\n";

        str += "\t\txml += '<tsms:"+fs.getXMLName()+">';\n";

        for(int i=0;i<fs.getFormStructur().size();i++) {
            FormStructur fs1 = fs.getFormStructur().get(i);
            if(fs1.isSimpleType()) {
                str += "\t\txml += '<tsms:"+fs1.getXMLName()+">'+this."+JSLib.getObjectField(fs1)+"+'</tsms:"+fs1.getXMLName()+">\\n';\n";
            }
            else {
                str += "\t\tfor(i"+fs.getId()+"=0;i"+fs.getId()+"<this."+JSLib.getObjectField(fs1)+".length;i"+fs.getId()+"++) {\n";
                    str += "\t\t"+JSLib.getLoggingFunction()+"('start "+JSLib.getObjectContructurName(fs)+" "+JSLib.getStructurAsXML()+":'+i"+fs.getId()+");\n";
                    str += "\t\t\txml += this."+JSLib.getObjectField(fs1)+"[i"+fs.getId()+"]."+JSLib.getStructurAsXML()+"();\n";
                str += "\t\t}\n";
            }
        }
        
        str += "\t\txml += '</tsms:"+fs.getXMLName()+">';\n";
        
        str += "\t\treturn xml;\n";
        str += "\t}\n";
        return str;
    }

    public static String getFromXmlToObjectFunction(FormStructur fs) {
        String str = "";

        str += "\tfunction " + JSLib.getFromXmlToObjectFunction(fs) +"(xml) {\n";

        str += "\t\t" + JSLib.getObjectField(fs) + "obj = new "+JSLib.getObjectContructurName(fs)+"();\n";
        str += "\t\t" + JSLib.getObjectField(fs) + "1str = '<"+fs.getXMLName()+">';\n";
        str += "\t\t" + JSLib.getObjectField(fs) + "2str = '</"+fs.getXMLName()+">';\n";
        str += "\t\tindex" + JSLib.getObjectField(fs) + "1length = xml.indexOf(" + JSLib.getObjectField(fs) + "1str);\n";
        str += "\t\tindex" + JSLib.getObjectField(fs) + "2length = xml.indexOf(" + JSLib.getObjectField(fs) + "2str);\n";
        str += "\t\t"+JSLib.getLoggingFunction()+"('xml:\\n'+xml+'\\nindex1:'+index" + JSLib.getObjectField(fs) + "1length+'\\nindex2:'+index" + JSLib.getObjectField(fs) + "2length);\n";
        str += "\t\tnewxml"+fs.getId()+" = xml.substring(index" + JSLib.getObjectField(fs) + "1length+" + JSLib.getObjectField(fs) + "1str.length,index" + JSLib.getObjectField(fs) + "2length);\n";
        for(int i=0;i<fs.getFormStructur().size();i++) {
            FormStructur fs1 = fs.getFormStructur().get(i);
            if(fs1.isSimpleType()) {
                str += "\t\t"+JSLib.getObjectField(fs1) + "1str = '<"+fs1.getXMLName()+">';\n";
                str += "\t\t"+JSLib.getObjectField(fs1) + "2str = '</"+fs1.getXMLName()+">';\n";
                str += "\t\tindex" + JSLib.getObjectField(fs1) + "1length = newxml"+fs.getId()+".indexOf(" + JSLib.getObjectField(fs1) + "1str);\n";
                str += "\t\tindex" + JSLib.getObjectField(fs1) + "2length = newxml"+fs.getId()+".indexOf(" + JSLib.getObjectField(fs1) + "2str);\n";
                str += "\t\tif(index" + JSLib.getObjectField(fs1) + "1length==-1 || index" + JSLib.getObjectField(fs1) + "2length != -2) {\n";
                str += "\t\t\txml" + fs.getId() + " = newxml" + fs.getId() + ".substring(index" + JSLib.getObjectField(fs1) + "1length+" + JSLib.getObjectField(fs1) + "1str.length,index" + JSLib.getObjectField(fs1) + "2length);\n";
                str += "\t\t\t"+JSLib.getObjectField(fs) + "obj."+JSLib.getObjectSetterName(fs1)+"(xml"+fs.getId() + ");\n";
                str += "\t\t}\n";
            }
            else {
                str += "\t\t"+JSLib.getObjectField(fs1) + "1str = '<"+fs1.getXMLName()+">';\n";
                str += "\t\t"+JSLib.getObjectField(fs1) + "2str = '</"+fs1.getXMLName()+">';\n";
                str += "\t\tindex" + JSLib.getObjectField(fs1) + "1length = newxml"+fs.getId()+".indexOf(" + JSLib.getObjectField(fs1) + "1str);\n";
                str += "\t\tindex" + JSLib.getObjectField(fs1) + "2length = newxml"+fs.getId()+".indexOf(" + JSLib.getObjectField(fs1) + "2str);\n";
                str += "\t\twhile(index" + JSLib.getObjectField(fs1) + "1length != -1) {\n";
                str += "\t\t"+JSLib.getLoggingFunction()+"('"+fs1.getXMLName()+" newxml"+fs.getId()+":\\n'+newxml"+fs.getId()+"+'\\nindex1:'+index" + JSLib.getObjectField(fs1) + "1length+'\\nindex2:'+index" + JSLib.getObjectField(fs1) + "2length);\n";
                str += "\t\t\tnewxml"+JSLib.getObjectField(fs1)+" = newxml"+fs.getId()+".substring(index" + JSLib.getObjectField(fs1) + "1length,index" + JSLib.getObjectField(fs1) + "2length+"+JSLib.getObjectField(fs1) + "2str.length);\n";
                str += "\t\t\tnewxmla = newxml"+fs.getId()+".substring(0, index" + JSLib.getObjectField(fs1) + "1length) + newxml"+fs.getId()+".substring(index" + JSLib.getObjectField(fs1) + "2length+"+JSLib.getObjectField(fs1) + "2str.length,newxml"+fs.getId()+".length);\n";

                str += "\t\t\tnewxml"+fs.getId()+" = newxmla;\n";

                str += "\t\t\t"+JSLib.getObjectField(fs1) + "obj1 = "+JSLib.getFromXmlToObjectFunction(fs1)+"(newxml"+JSLib.getObjectField(fs1)+");\n";
                str += "\t\t\t"+JSLib.getObjectField(fs) + "obj."+JSLib.getObjectAddList(fs1)+"("+JSLib.getObjectField(fs1) + "obj1);\n";
                str += "\t\tindex" + JSLib.getObjectField(fs1) + "1length = newxml"+fs.getId()+".indexOf(" + JSLib.getObjectField(fs1) + "1str);\n";
                str += "\t\tindex" + JSLib.getObjectField(fs1) + "2length = newxml"+fs.getId()+".indexOf(" + JSLib.getObjectField(fs1) + "2str);\n";
                str += "\t\t}\n";
            }
        }
        str += "\t\treturn " + JSLib.getObjectField(fs) + "obj;\n";
        str += "\t}\n";

        return str;
    }
}